package com.freesky.web.manager.service.impl;

import com.freesky.common.exception.EMallException;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.dao.entity.Thanks;
import com.freesky.web.manager.service.ThanksService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Freesky
 */
@Service
@Slf4j
public class ThanksServiceImpl implements ThanksService {
    @Override
    public DataTablesResult getThanksList() {
        DataTablesResult result=new DataTablesResult();
        List<Thanks> list = new Thanks().selectAll();
        if(list==null){
            throw new EMallException("获取捐赠列表失败");
        }
        result.setSuccess(true);
        result.setData(list);
        return result;
    }

    @Override
    public DataTablesResult getThanksListByPage(int page, int size) {

        DataTablesResult result=new DataTablesResult();
        if(page<=0) {
            page = 1;
        }

        Page<Thanks> thanksPage = new Thanks().selectPage(new Page<>(page, size), new EntityWrapper<>());
        if(thanksPage.getRecords()==null){
            throw new EMallException("获取捐赠列表失败");
        }
        for(Thanks thanks:thanksPage.getRecords()){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = null;
            try {
                date = dateFormat.format(thanks.getDate());
            } catch (Exception e) {
                e.printStackTrace();
            }
            thanks.setTime(date);
            thanks.setEmail(null);
        }

        result.setSuccess(true);
        result.setRecordsTotal(thanksPage.getTotal());
        result.setData(thanksPage.getRecords());
        return result;
    }

    @Override
    public JSONResponse countThanks() {
        return JSONResponseDiretor.buildSuccessJSONResponse(new Thanks().selectCount(new EntityWrapper()));
    }

    @Override
    public JSONResponse addThanks(Thanks thanks) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(thanks.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        thanks.setDate(date);
        boolean insert = thanks.insert();
        if(!insert){
            throw new EMallException("添加捐赠失败");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse updateThanks(Thanks thanks) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(thanks.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        thanks.setDate(date);

        if(! thanks.updateById()){
            throw new EMallException("更新捐赠失败");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public boolean deleteThanks(int id) {
        Thanks thanks = new Thanks(id);
        boolean delete = false;
        try {
             delete = thanks.deleteById();
        } catch (Exception e) {
            log.error("删除捐赠失败",e);
        }
        return delete;
    }

    @Override
    public JSONResponse getThankById(int id) {

        Thanks thanks=new Thanks(id);
        thanks = thanks.selectById();
        if(thanks==null){
            throw new EMallException("获取捐赠数据失败");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(thanks);
    }
}
